package com.testfileupload.uploadimg.design;

public class DependencyInversion2 {
    public static void main(String[] args) {
//        IOpenOrClose openAndClose = new OpenAndClose();
//        ITv iTv = new Tv();
//        openAndClose.open(iTv);

//        ITv iTv = new Tv();
//        IOpenOrClose openAndClose = new OpenAndClose(iTv);
//        openAndClose.open();

        ITv iTv = new Tv();
        IOpenOrClose openAndClose = new OpenAndClose();
        openAndClose.setTv(iTv);
        openAndClose.open();
    }
}

/**
 * 接口传递实现依赖
 * <p>
 * 通过构造方法依赖传递
 * <p>
 * 通过构造方法依赖传递
 */
//interface IOpenOrClose {
//    public void open(ITv itv);
//}
//
//interface ITv {
//    public void play();
//}
//
//class Tv implements ITv {
//    @Override
//    public void play() {
//        System.out.println("播放 1");
//    }
//}
//
//class OpenAndClose implements IOpenOrClose {
//    @Override
//    public void open(ITv itv) {
//        itv.play();
//    }
//}

/**
 * 通过构造方法依赖传递
 */
//
//interface IOpenOrClose {
//    public void open();
//}
//
//interface ITv {
//    public void play();
//}
//
//class Tv implements ITv {
//    @Override
//    public void play() {
//        System.out.println("播放 2");
//    }
//}
//
//class OpenAndClose implements IOpenOrClose {
//    private ITv iTv;
//
//    public OpenAndClose(ITv itv) {
//        this.iTv = itv;
//    }
//
//    @Override
//    public void open() {
//        iTv.play();
//    }
//}

/**
 * 通过setter
 */
interface IOpenOrClose {
    public void open();

    public void setTv(ITv iTv);
}

interface ITv {
    public void play();
}

class Tv implements ITv {
    @Override
    public void play() {
        System.out.println("播放 3");
    }
}

class OpenAndClose implements IOpenOrClose {
    private ITv iTv;

    @Override
    public void open() {
        iTv.play();
    }

    @Override
    public void setTv(ITv iTv) {
        this.iTv = iTv;
    }
}